package com.hilaver.dzmis.service.impl;

import java.util.List;
import java.util.Locale;
import java.util.Set;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiArmor;
import com.hilaver.dzmis.basicinfo.BiYarn;
import com.hilaver.dzmis.codetable.CtAccessoryType;
import com.hilaver.dzmis.codetable.CtArmor1Type;
import com.hilaver.dzmis.codetable.CtArmorSet;
import com.hilaver.dzmis.codetable.CtArmorType;
import com.hilaver.dzmis.codetable.CtCollarType;
import com.hilaver.dzmis.codetable.CtMsiDescription;
import com.hilaver.dzmis.codetable.CtProductIdentificationStyle;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.codetable.CtStature;
import com.hilaver.dzmis.codetable.CtVersion;
import com.hilaver.dzmis.codetable.CtYarnMaterial;
import com.hilaver.dzmis.codetable.CtYarnMeasure;
import com.hilaver.dzmis.dao.ProductIdentificationDAO;
import com.hilaver.dzmis.order.OrderColorTest;
import com.hilaver.dzmis.product.ProductAccessoryHang;
import com.hilaver.dzmis.product.ProductAccessoryOthers;
import com.hilaver.dzmis.product.ProductDZColor;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductLaunching;
import com.hilaver.dzmis.product.ProductMeasureSpecificationItem;
import com.hilaver.dzmis.service.BasePDFService;
import com.hilaver.dzmis.util.StringUtils;
import com.lowagie.text.Document;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.FontSelector;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;

public class PDFExportServiceImpl extends BasePDFService{

	private Locale locale;

	private ProductIdentificationDAO dao;

	private BiDZColorServiceImpl colorServiceImpl;

	private CtServiceImpl ctService;

	private Document document;

	public PDFExportServiceImpl() {
	}

	public PDFExportServiceImpl(Document document, Locale locale) {
		super.locale = locale;
		this.dao = new ProductIdentificationDAO();
		this.colorServiceImpl = new BiDZColorServiceImpl();
		this.ctService = new CtServiceImpl();
		super.i18n = new I18nServiceImpl();
		this.document = document;
		this.setPageConfig();
	}

	public void exportProductionIdentification(Integer id) throws Exception {
		ProductIdentification pi = (ProductIdentification) this.dao.get(
				ProductIdentification.class.getName(), id);

		// Add title Table
		document.add(this.getPiTitleTable(pi, "ui.productidentification.pi"));
		// Add armor
		document.add(this.getPiArmorTable(pi));
		// Add Yarn
		document.add(this.getPiYarnTable(pi));
		// Add skecth
		document.add(this.getBiSketchTable(pi));
		// Add dz color and experiment
		document.add(this.getBiColorTable(pi));

		document.newPage();

		document.add(this.getPiTitleTable1(pi,
				"ui.productidentification.measurespecification"));

		document.add(this.getBiMeasureSpecificationTable(pi));

		document.newPage();

		document.add(this.getPiTitleTable2(pi, "ui.procurement.accessory"));

		document.add(this.getTrimmingTable(pi));
	}

	private void setPageConfig() {
		this.document.setPageSize(PageSize.A4);
		this.document.setMargins(30, 30, 35, 35);
		HeaderFooter header = new HeaderFooter(new Phrase("Design Zone"), false);
		header.setBorder(Rectangle.NO_BORDER);
		header.setAlignment(Paragraph.ALIGN_RIGHT);
		header.setRight(140);
		this.document.setHeader(header);
		this.selector = new FontSelector();
		// selector.addFont(FontFactory.getFont(FontFactory.COURIER, 10));
		selector.addFont(FontFactory.getFont("Arial", 9));
		selector.addFont(FontFactory.getFont("STSong-Light", "UniGB-UCS2-H",
				BaseFont.NOT_EMBEDDED, 9));
	}

	private PdfPTable getPiTitleTable(ProductIdentification pi, String titleKey)
			throws Exception {
		// first page
		float[] widthsTitle = { 2f, 2.2f, 2.3f, 1.5f, 1f };
		PdfPTable table1 = new PdfPTable(widthsTitle);
		table1.setWidthPercentage(100);
		table1.setSpacingBefore(2);
		// 1st row
		table1.addCell(getTableCell(titleKey, false, 5));
		table1.addCell(getEmptyLine(5));
		// 2nd row
		table1.addCell(getTableCell("ui.productidentification.style", false, 1,
				Paragraph.ALIGN_RIGHT));
		table1.addCell(getTableCell(new String[] {
				this.ctService.getDescription(CtCollarType.class.getName(), pi
						.getCtCollarType(), locale),
				this.ctService.getDescription(
						CtProductIdentificationStyle.class.getName(), pi
								.getCtProductIdentificationStyle(), locale) },
				true, 4));
		table1.addCell(getEmptyLine(5));
		// 3nd row
		table1.addCell(getTableCell("ui.francecustomer.name", false, 1,Paragraph.ALIGN_RIGHT));
		table1.addCell(getTableCell(new String[] {pi.getBiFranceCustomer().getName(),pi.getBiFranceCustomer().getDepartment() }, true, 1));
		table1.addCell(getTableCell("", false, 1));
		table1.addCell(getTableCell("ui.productidentification.seasonnumber",
				true, 1));
		table1.addCell(getTableCell(
				this.ctService.getDescription(CtSeasonNumber.class.getName(),
						pi.getCtSeasonNumber(), locale), true, 1));
		table1.addCell(getEmptyLine(5));
		// 4th row
//		table1.addCell(getTableCell("ui.francecustomer.contactrehearsals",
//				false, 1, Paragraph.ALIGN_RIGHT));
//		table1.addCell(getTableCell(pi.getBiFranceCustomer()
//				.getContactRehearsals(), true, 1));
		table1.addCell(getTableCell("ui.productidentification.chartversion",
				false, 1));
		table1
				.addCell(getTableCell(this.ctService.getDescription(
						CtVersion.class.getName(), pi.getCtVersion(), locale),
						true, 1));
		
		table1.addCell(getTableCell("ui.reference.reference", false, 1,
				Paragraph.ALIGN_RIGHT));
		table1.addCell(getTableCell(pi.getReference(), true, 1));
		table1.addCell(getTableCell("", false, 1));
		table1.addCell(getEmptyLine(5));
		// 5th row
//		table1.addCell(getTableCell("ui.measurespecification.stature", false,
//				1, Paragraph.ALIGN_RIGHT));
//		table1.addCell(getTableCell(this.ctService.getDescription(
//				CtStature.class.getName(), pi.getBiMeasureSpecification()
//						.getCtStature(), locale), true, 1));
		table1.addCell(getTableCell("ui.measurespecification.stature", false,
				1, Paragraph.ALIGN_RIGHT));
		table1.addCell(getTableCell(this.ctService.getDescription(
				CtStature.class.getName(), pi.getBiMeasureSpecification()
						.getCtStature(), locale), true, 1));
		table1.addCell(getTableCell(new String[] { "message.edit.createdate",
				"(d/m/Y)" }, false, 1, Paragraph.ALIGN_RIGHT));
		table1.addCell(getTableCell(StringUtils.toString(pi.getCreateDate()),
				true, 1));
		table1.addCell(getTableCell("", false, 1));
		table1.addCell(getEmptyLine(5));
		// 6th row
//		table1.addCell(getTableCell("", false, 1));
//		table1.addCell(getTableCell("", false, 1));
//		table1.addCell(getTableCell("ui.productidentification.chartversion",
//				false, 1));
//		table1
//				.addCell(getTableCell(this.ctService.getDescription(
//						CtVersion.class.getName(), pi.getCtVersion(), locale),
//						true, 1));
//		table1.addCell(getTableCell("", false, 1));
//		table1.addCell(getEmptyLine(5));
		// 7th row
//		table1.addCell(getTableCell("ui.procurement.accessory", false, 1,
//				Paragraph.ALIGN_RIGHT));
//		table1.addCell(getTableCell("", true, 1));
//		table1.addCell(getTableCell("ui.measurespecification.patronage", false,
//				1));
//		table1.addCell(getTableCell(pi.getBiMeasureSpecification()
//				.getPatronage(), true, 1));
//		table1.addCell(getTableCell("", false, 1));
//		table1.addCell(getEmptyLine(5));

		return table1;
	}
	
	private PdfPTable getPiTitleTable1(ProductIdentification pi, String titleKey)
		throws Exception {
		// first page
		float[] widthsTitle = { 2f, 2.2f, 2.3f, 1.5f, 1f };
		PdfPTable table1 = new PdfPTable(widthsTitle);
		table1.setWidthPercentage(100);
		table1.setSpacingBefore(2);
		// 1st row
		//table1.addCell(getTableCell(titleKey, false, 5));
		//table1.addCell(getEmptyLine(5));
		// 2nd row
//		table1.addCell(getTableCell("ui.productidentification.style", false, 1,
//				Paragraph.ALIGN_RIGHT));
//		table1.addCell(getTableCell(new String[] {
//				this.ctService.getDescription(CtCollarType.class.getName(), pi
//						.getCtCollarType(), locale),
//				this.ctService.getDescription(
//						CtProductIdentificationStyle.class.getName(), pi
//								.getCtProductIdentificationStyle(), locale) },
//				true, 4));
//		table1.addCell(getEmptyLine(5));
		// 3nd row
		//table1.addCell(getTableCell("ui.francecustomer.name", false, 1,Paragraph.ALIGN_RIGHT));
		//table1.addCell(getTableCell(new String[] {pi.getBiFranceCustomer().getName(),pi.getBiFranceCustomer().getDepartment() }, true, 1));
//		table1.addCell(getTableCell("ui.productidentification.chartversion",
//				false, 1));
//		table1
//				.addCell(getTableCell(this.ctService.getDescription(
//						CtVersion.class.getName(), pi.getCtVersion(), locale),
//						true, 1));
		table1.addCell(getTableCell("ui.measurespecification.patronage", false,
				1));
		table1.addCell(getTableCell(pi.getBiMeasureSpecification()
				.getPatronage(), true, 1));
		table1.addCell(getTableCell("", false, 1));
		table1.addCell(getTableCell("ui.productidentification.seasonnumber",
				true, 1));
		table1.addCell(getTableCell(
				this.ctService.getDescription(CtSeasonNumber.class.getName(),
						pi.getCtSeasonNumber(), locale), true, 1));
		table1.addCell(getEmptyLine(5));
		// 4th row
		table1.addCell(getTableCell("ui.measurespecification.stature", false,
				1, Paragraph.ALIGN_RIGHT));
		table1.addCell(getTableCell(this.ctService.getDescription(
				CtStature.class.getName(), pi.getBiMeasureSpecification()
						.getCtStature(), locale), true, 1));
		table1.addCell(getTableCell("ui.reference.reference", false, 1,
				Paragraph.ALIGN_RIGHT));
		table1.addCell(getTableCell(pi.getReference(), true, 1));
		table1.addCell(getTableCell("", false, 1));
		table1.addCell(getEmptyLine(5));
		// 5th row
//		table1.addCell(getTableCell("ui.measurespecification.stature", false,
//				1, Paragraph.ALIGN_RIGHT));
//		table1.addCell(getTableCell(this.ctService.getDescription(
//				CtStature.class.getName(), pi.getBiMeasureSpecification()
//						.getCtStature(), locale), true, 1));
//		table1.addCell(getTableCell(new String[] { "message.edit.createdate",
//				"(d/m/Y)" }, false, 1, Paragraph.ALIGN_RIGHT));
//		table1.addCell(getTableCell(StringUtils.toString(pi.getCreateDate()),
//				true, 1));
//		table1.addCell(getTableCell("", false, 1));
//		table1.addCell(getEmptyLine(5));
		// 6th row
		//table1.addCell(getTableCell("", false, 1));
		//table1.addCell(getTableCell("", false, 1));
		//table1.addCell(getTableCell("ui.productidentification.chartversion",
		//		false, 1));
		//table1
		//		.addCell(getTableCell(this.ctService.getDescription(
		//				CtVersion.class.getName(), pi.getCtVersion(), locale),
		//				true, 1));
		//table1.addCell(getTableCell("", false, 1));
		//table1.addCell(getEmptyLine(5));
		// 7th row
		//table1.addCell(getTableCell("ui.procurement.accessory", false, 1,
		//		Paragraph.ALIGN_RIGHT));
		//table1.addCell(getTableCell("", true, 1));
		//table1.addCell(getTableCell("ui.measurespecification.patronage", false,
		//		1));
		//table1.addCell(getTableCell(pi.getBiMeasureSpecification()
		//		.getPatronage(), true, 1));
		//table1.addCell(getTableCell("", false, 1));
		//table1.addCell(getEmptyLine(5));
		
		return table1;
	}
	
	private PdfPTable getPiTitleTable2(ProductIdentification pi, String titleKey)
	throws Exception {
		// first page
		float[] widthsTitle = { 2f, 2.2f, 2.3f, 1.5f, 1f };
		PdfPTable table1 = new PdfPTable(widthsTitle);
		table1.setWidthPercentage(100);
		table1.setSpacingBefore(2);
		// 1st row
		table1.addCell(getTableCell(titleKey, false, 5));
		table1.addCell(getEmptyLine(5));
		// 2nd row
		table1.addCell(getTableCell("ui.productidentification.style", false, 1,
				Paragraph.ALIGN_RIGHT));
		table1.addCell(getTableCell(new String[] {
				this.ctService.getDescription(CtCollarType.class.getName(), pi
						.getCtCollarType(), locale),
				this.ctService.getDescription(
						CtProductIdentificationStyle.class.getName(), pi
								.getCtProductIdentificationStyle(), locale) },
				true, 4));
		table1.addCell(getEmptyLine(5));
		// 3nd row
		table1.addCell(getTableCell("ui.francecustomer.name", false, 1,Paragraph.ALIGN_RIGHT));
		table1.addCell(getTableCell(new String[] {pi.getBiFranceCustomer().getName(),pi.getBiFranceCustomer().getDepartment() }, true, 1));
		table1.addCell(getTableCell("", false, 1));
		table1.addCell(getTableCell("ui.productidentification.seasonnumber",
				true, 1));
		table1.addCell(getTableCell(
				this.ctService.getDescription(CtSeasonNumber.class.getName(),
						pi.getCtSeasonNumber(), locale), true, 1));
		table1.addCell(getEmptyLine(5));
		// 4th row
		//table1.addCell(getTableCell("ui.francecustomer.contactrehearsals",
		//		false, 1, Paragraph.ALIGN_RIGHT));
		//table1.addCell(getTableCell(pi.getBiFranceCustomer()
		//		.getContactRehearsals(), true, 1));
//		table1.addCell(getTableCell("ui.productidentification.chartversion",
//				false, 1));
//		table1
//				.addCell(getTableCell(this.ctService.getDescription(
//						CtVersion.class.getName(), pi.getCtVersion(), locale),
//						true, 1));
		table1.addCell(getTableCell("", false, 1));
		table1.addCell(getTableCell("", false, 1));
		table1.addCell(getTableCell("ui.reference.reference", false, 1,
				Paragraph.ALIGN_RIGHT));
		table1.addCell(getTableCell(pi.getReference(), true, 1));
		table1.addCell(getTableCell("", false, 1));
		table1.addCell(getEmptyLine(5));
		// 5th row
		//table1.addCell(getTableCell("ui.measurespecification.stature", false,
		//		1, Paragraph.ALIGN_RIGHT));
		//table1.addCell(getTableCell(this.ctService.getDescription(
		//		CtStature.class.getName(), pi.getBiMeasureSpecification()
		//				.getCtStature(), locale), true, 1));
//		table1.addCell(getTableCell("ui.measurespecification.stature", false,
//				1, Paragraph.ALIGN_RIGHT));
//		table1.addCell(getTableCell(this.ctService.getDescription(
//				CtStature.class.getName(), pi.getBiMeasureSpecification()
//						.getCtStature(), locale), true, 1));
//		table1.addCell(getTableCell(new String[] { "message.edit.createdate",
//				"(d/m/Y)" }, false, 1, Paragraph.ALIGN_RIGHT));
//		table1.addCell(getTableCell(StringUtils.toString(pi.getCreateDate()),
//				true, 1));
//		table1.addCell(getTableCell("", false, 1));
//		table1.addCell(getEmptyLine(5));
		// 6th row
		//table1.addCell(getTableCell("", false, 1));
		//table1.addCell(getTableCell("", false, 1));
		//table1.addCell(getTableCell("ui.productidentification.chartversion",
		//		false, 1));
		//table1
		//		.addCell(getTableCell(this.ctService.getDescription(
		//				CtVersion.class.getName(), pi.getCtVersion(), locale),
		//				true, 1));
		//table1.addCell(getTableCell("", false, 1));
		//table1.addCell(getEmptyLine(5));
		// 7th row
		//table1.addCell(getTableCell("ui.procurement.accessory", false, 1,
		//		Paragraph.ALIGN_RIGHT));
		//table1.addCell(getTableCell("", true, 1));
		//table1.addCell(getTableCell("ui.measurespecification.patronage", false,
		//		1));
		//table1.addCell(getTableCell(pi.getBiMeasureSpecification()
		//		.getPatronage(), true, 1));
		//table1.addCell(getTableCell("", false, 1));
		//table1.addCell(getEmptyLine(5));
		
		return table1;
		}

	private PdfPTable getPiArmorTable(ProductIdentification pi)
			throws Exception {
		float[] widthsArmor = { 1f, 1f, 1f, 1.5f, 1.5f, 1.5f, 1.5f };
		PdfPTable table2 = new PdfPTable(widthsArmor);
		table2.setWidthPercentage(100);
		PdfPCell cellArmor1 = getTableCell("ui.armor.armor", true, 1);
		cellArmor1.setRowspan(2);
		cellArmor1.setPaddingTop(10);
		table2.addCell(cellArmor1);
		table2.addCell(getTableCell("ui.reference.front", true, 1));
		table2.addCell(getTableCell("ui.reference.back", true, 1));
		table2.addCell(getTableCell("ui.reference.sleeves", true, 1));
		table2.addCell(getTableCell("ui.reference.bottomthrum", true, 1));
		table2.addCell(getTableCell("ui.reference.sleevesthrum", true, 1));
		table2.addCell(getTableCell("ui.reference.necklinethrum", true, 1));

		// Ext.getCmp(currentAomor).setValue(ctArmorType + ' ' + ctArmor1Type +
		// ' ' + armorTypeE + ' ' + ctArmorSet);
		String front = "";
		if (pi.getFront() != null) {
			front += this.ctService.getDescription(CtArmorType.class.getName(),
					pi.getFront().getCtArmorType(), locale)
					+ " ";
			front += this.ctService.getDescription(
					CtArmor1Type.class.getName(), pi.getFront()
							.getCtArmor1Type(), locale)
					+ " ";
			front += pi.getFront().getArmorTypeE() == null ? "" : (pi
					.getFront().getArmorTypeE() + " ");
			front += this.ctService.getDescription(CtArmorSet.class.getName(),
					pi.getFront().getCtArmorSet(), locale)
					+ " ";
		}
		table2.addCell(getTableCell(front, true, 3));

		String bottomThrum = "";
		BiArmor biArmor = null;
		if (pi.getBottomThrum() != null) {
			biArmor = pi.getBottomThrum();
		}
		if (pi.getSleevesThrum() != null) {
			biArmor = pi.getSleevesThrum();
		}
		if (pi.getNecklineThrum() != null) {
			biArmor = pi.getNecklineThrum();
		}
		if (biArmor != null) {
			bottomThrum += this.ctService.getDescription(CtArmorType.class
					.getName(), biArmor.getCtArmorType(), locale)
					+ " ";
			bottomThrum += this.ctService.getDescription(CtArmor1Type.class
					.getName(), biArmor.getCtArmor1Type(), locale)
					+ " ";
			bottomThrum += pi.getFront().getArmorTypeE() == null ? ""
					: (biArmor.getArmorTypeE() + " ");
			bottomThrum += this.ctService.getDescription(CtArmorSet.class
					.getName(), biArmor.getCtArmorSet(), locale)
					+ " ";
		}
		table2.addCell(getTableCell(bottomThrum, true, 3));
		table2.addCell(getEmptyLine(7));
		return table2;
	}

	private PdfPTable getPiYarnTable(ProductIdentification pi) throws Exception {
		float[] widthsYarn = { 1f, 1f, 1f, 1f, 1.5f, 1f, 1.5f };
		PdfPTable table3 = new PdfPTable(widthsYarn);
		table3.setWidthPercentage(100);
		table3.addCell(getTableCell("message.edit.jauge", true, 1));
		table3.addCell(getTableCell(pi.getJauge(), true, 1));
		table3.addCell(getTableCell("ui.reference.machinetype", true, 1));
		table3
				.addCell(getTableCell("ui.factory." + pi.getEquipment(), true,
						1));
		table3.addCell(getTableCell(new String[] { "ui.reference.weight",
				" (Kg)" }, true, 1));
		table3.addCell(getTableCell(pi.getWeight() == null ? "" : pi
				.getWeight().toString(), true, 1));
		table3.addCell(getTableCell(new String[] {
				"ui.productidentification.precentage", " (%)" }, true, 1));

		Set<ProductDZColor> pCZColorSet = pi.getProductDZColors();
		String variant = "";
		int colorIndex = 1;
		for (ProductDZColor pDZColor : pCZColorSet) {
			if (colorIndex == 1) {
				variant = pDZColor.getVariant();
			}
			if (variant.equals(pDZColor.getVariant())) {
				table3.addCell(getTableCell("ui.yarn.fil", true, 1));
				BiYarn biYarn = pDZColor.getBiDZColor().getBiYarn();
				table3.addCell(getTableCell(new String[] {
						this.ctService.getDescription(CtYarnMeasure.class
								.getName(), biYarn.getCtYarnMeasure(), locale),
						biYarn.getYarnAccount() }, true, 1));
				table3.addCell(getTableCell("ui.yarn.composition", true, 1));
				String composition = "";
				if (!StringUtils.isEmpty(biYarn.getCtYarnMaterial1())) {
					composition += biYarn.getComposition1()
							+ "% "
							+ this.ctService.getDescription(
									CtYarnMaterial.class.getName(), biYarn
											.getCtYarnMaterial1(), locale)
							+ "  ";
				}
				if (!StringUtils.isEmpty(biYarn.getCtYarnMaterial2())) {
					composition += biYarn.getComposition2()
							+ "% "
							+ this.ctService.getDescription(
									CtYarnMaterial.class.getName(), biYarn
											.getCtYarnMaterial2(), locale)
							+ "  ";
				}
				if (!StringUtils.isEmpty(biYarn.getCtYarnMaterial3())) {
					composition += biYarn.getComposition3()
							+ "% "
							+ this.ctService.getDescription(
									CtYarnMaterial.class.getName(), biYarn
											.getCtYarnMaterial3(), locale)
							+ "  ";
				}
				if (!StringUtils.isEmpty(biYarn.getCtYarnMaterial4())) {
					composition += biYarn.getComposition4()
							+ "% "
							+ this.ctService.getDescription(
									CtYarnMaterial.class.getName(), biYarn
											.getCtYarnMaterial4(), locale)
							+ "  ";
				}
				if (!StringUtils.isEmpty(biYarn.getCtYarnMaterial5())) {
					composition += biYarn.getComposition5()
							+ "% "
							+ this.ctService.getDescription(
									CtYarnMaterial.class.getName(), biYarn
											.getCtYarnMaterial5(), locale)
							+ "  ";
				}
				table3.addCell(getTableCell(composition, true, 3));
				table3.addCell(getTableCell(
						pDZColor.getPercentage().toString(), true, 1));
			}
			colorIndex++;
		}
		table3.addCell(getTableCell("ui.yarn.fil", true, 1));
		table3.addCell(getTableCell("", true, 1));
		table3.addCell(getTableCell("ui.yarn.composition", true, 1));
		table3.addCell(getTableCell("", true, 3));
		table3.addCell(getTableCell("", true, 1));
		table3.addCell(getEmptyLine(7));
		return table3;
	}

	private PdfPTable getBiSketchTable(ProductIdentification pi)
			throws Exception {
		float[] widthsSketch = { 0.2f, 1f, 1f, 1f, 0.2f };
		PdfPTable table4 = new PdfPTable(widthsSketch);
		table4.setWidthPercentage(100);
		table4.addCell(getTableCell("", true, 1));
		if (pi.getSketch1() != null) {
			table4.addCell(Image.getInstance(super.realFileDir
					+ pi.getSketch1Name()));
		} else {
			table4.addCell(getTableCell("", true, 1));
		}
		if (pi.getSketch2() != null) {
			table4.addCell(Image.getInstance(super.realFileDir
					+ pi.getSketch2Name()));
		} else {
			table4.addCell(getTableCell("", true, 1));
		}
		if (pi.getSketch3() != null) {
			table4.addCell(Image.getInstance(super.realFileDir
					+ pi.getSketch3Name()));
		} else {
			table4.addCell(getTableCell("", true, 1));
		}
		table4.addCell(getTableCell("", true, 1));

		table4.addCell(getTableCell("", true, 1));
		if (Constants.LOCALE_TYPE_EN.equals(locale)) {
			table4.addCell(getTableCell(pi.getSketch1DescriptionEN(), true, 1));
		} else if (Constants.LOCALE_TYPE_ZH.equals(locale)) {
			table4.addCell(getTableCell(pi.getSketch1DescriptionCN(), true, 1));
		} else {
			table4.addCell(getTableCell(pi.getSketch1DescriptionFR(), true, 1));
		}
		if (Constants.LOCALE_TYPE_EN.equals(locale)) {
			table4.addCell(getTableCell(pi.getSketch2DescriptionEN(), true, 1));
		} else if (Constants.LOCALE_TYPE_ZH.equals(locale)) {
			table4.addCell(getTableCell(pi.getSketch2DescriptionCN(), true, 1));
		} else {
			table4.addCell(getTableCell(pi.getSketch2DescriptionFR(), true, 1));
		}
		if (Constants.LOCALE_TYPE_EN.equals(locale)) {
			table4.addCell(getTableCell(pi.getSketch3DescriptionEN(), true, 1));
		} else if (Constants.LOCALE_TYPE_ZH.equals(locale)) {
			table4.addCell(getTableCell(pi.getSketch3DescriptionCN(), true, 1));
		} else {
			table4.addCell(getTableCell(pi.getSketch3DescriptionFR(), true, 1));
		}
		table4.addCell(getTableCell("", true, 1));
		table4.addCell(getEmptyLine(5));
		return table4;
	}

	private PdfPTable getBiColorTable(ProductIdentification pi)
			throws Exception {
		float[] widthsColor = { 0.5f, 1f, 1f, 1f, 1.5f, 1.5f };
		PdfPTable table5 = new PdfPTable(widthsColor);
		table5.setWidthPercentage(100);

		table5.addCell(getTableCell("ui.dzcolor.variant", true, 1));
		table5.addCell(getTableCell("ui.order.color", true, 1));
		table5.addCell(getTableCell("ui.order.colortest", true, 1));
		table5.addCell(getTableCell("ui.productidentification.quantitysweater",
				true, 1));
		table5.addCell(getTableCell(new String[] {
				"ui.productidentification.quantityswatch", " (50x50cm)" },
				true, 1));
		table5.addCell(getTableCell("message.edit.comment", true, 1));

		List<String> variantList = this.dao.getDifferentVariant(pi.getId());

		Set<ProductDZColor> pDZColorSet = pi.getProductDZColors();
		Set<ProductLaunching> plSet = pi.getProductLaunchings();
		for (int i = 0; i < variantList.size(); i++) {
			int rowSpan = 0;
			for (ProductDZColor pDZColor1 : pDZColorSet) {
				if (variantList.get(i).equals(pDZColor1.getVariant())) {
					rowSpan++;
				}
			}
			PdfPCell cell51 = getTableCell(variantList.get(i), true, 1);
			cell51.setRowspan(rowSpan);
			table5.addCell(cell51);

			int hit = 0;
			for (ProductDZColor pDZColor : pDZColorSet) {
				if (variantList.get(i).equals(pDZColor.getVariant())) {
					table5.addCell(getTableCell(pDZColor.getBiDZColor()
							.getCustomerColorCode(), true, 1));

					OrderColorTest oct = colorServiceImpl.getColorText(pDZColor
							.getBiDZColor());
					if (oct == null) {
						table5.addCell(getTableCell("", true, 1));
					} else {
						if ("01".equals(oct.getIsColorApproved())) {
							table5.addCell(getTableCell(new String[] {
									oct.getTestNumber(), "OK" }, true, 1));
						} else {
							table5.addCell(getTableCell("NO", true, 1));
						}
					}

					if (hit > 0) {
						continue;
					}

					for (ProductLaunching productLaunching : plSet) {
						if (variantList.get(i).equals(
								productLaunching.getVariant())) {
							hit++;
							PdfPCell cell54 = getTableCell(productLaunching
									.getQuantity1() == null ? ""
									: productLaunching.getQuantity1()
											.toString(), true, 1);
							cell54.setRowspan(rowSpan);
							table5.addCell(cell54);

							PdfPCell cell55 = getTableCell(productLaunching
									.getQuantity2() == null ? ""
									: productLaunching.getQuantity2()
											.toString(), true, 1);
							cell55.setRowspan(rowSpan);
							table5.addCell(cell55);

							PdfPCell cell56 = getTableCell("", true, 1);
							cell56.setRowspan(rowSpan);
							table5.addCell(cell56);
							break;
						}
					}

					if (hit == 0) {
						hit++;
						PdfPCell cell54 = getTableCell("", true, 1);
						cell54.setRowspan(rowSpan);
						table5.addCell(cell54);

						PdfPCell cell55 = getTableCell("", true, 1);
						cell55.setRowspan(rowSpan);
						table5.addCell(cell55);

						PdfPCell cell56 = getTableCell("", true, 1);
						cell56.setRowspan(rowSpan);
						table5.addCell(cell56);
					}
				}
			}

		}

		return table5;
	}

	private PdfPTable getBiMeasureSpecificationTable(ProductIdentification pi)
			throws Exception {
		float[] widthsSketch = { 0.5f, 2.0f, 1f, 0.9f, 1f, 1f, 1f, 1.1f, 1.2f,
				1.4f, 1.2f };
		PdfPTable table6 = new PdfPTable(widthsSketch);
		table6.setWidthPercentage(100);

		table6
				.addCell(getTableCell("ui.measurespecification.tailles", true,
						10));
		table6.addCell(getTableCell("L", true, 1));

		PdfPCell cell21 = getTableCell("N°", true, 1);
		cell21.setRowspan(2);
		table6.addCell(cell21);
		PdfPCell cell22 = getTableCell(
				"ui.measurespecification.itemdesignations", true, 1);
		cell22.setRowspan(2);
		table6.addCell(cell22);

		table6.addCell(getTableCell("ui.quality.measure", true, 8));

		PdfPCell cell23 = getTableCell("ui.measurespecification.tolerance",
				true, 1);
		cell23.setRowspan(2);
		table6.addCell(cell23);

		String[] sizeNames = BiMeasureSpecificationServiceImpl.getSizeName(pi
				.getBiMeasureSpecification().getCtStature());
		for (int i = 0; i < sizeNames.length; i++) {
			table6.addCell(getTableCell(sizeNames[i], true, 1));
		}

		Set<ProductMeasureSpecificationItem> pMSItems = pi
				.getProductMeasureSpecificationItems();
		for (char j = 'A'; j <= 'U'; j++) {
			int hit = 0;
			for (ProductMeasureSpecificationItem pMSItem : pMSItems) {
				if (String.valueOf(j).equals(pMSItem.getNumber())) {
					hit++;
					table6.addCell(getTableCell(String.valueOf(j), true, 1));
					table6.addCell(getTableCell(this.ctService.getDescription(
							CtMsiDescription.class.getName(), pMSItem
									.getCtMsiDescription(), locale), true, 1));
					table6.addCell(getTableCell(pMSItem.getSize1() == null ? ""
							: pMSItem.getSize1().toString(), true, 1));
					table6.addCell(getTableCell(pMSItem.getSize2() == null ? ""
							: pMSItem.getSize2().toString(), true, 1));
					table6.addCell(getTableCell(pMSItem.getSize3() == null ? ""
							: pMSItem.getSize3().toString(), true, 1));
					table6.addCell(getTableCell(pMSItem.getSize4() == null ? ""
							: pMSItem.getSize4().toString(), true, 1));
					table6.addCell(getTableCell(pMSItem.getSize5() == null ? ""
							: pMSItem.getSize5().toString(), true, 1));
					table6.addCell(getTableCell(pMSItem.getSize6() == null ? ""
							: pMSItem.getSize6().toString(), true, 1));
					table6.addCell(getTableCell(pMSItem.getSize7() == null ? ""
							: pMSItem.getSize7().toString(), true, 1));
					table6.addCell(getTableCell(pMSItem.getSize8() == null ? ""
							: pMSItem.getSize8().toString(), true, 1));
					table6.addCell(getTableCell(
							pMSItem.getTolerance() == null ? ""
									: ("+/-" + pMSItem.getTolerance()
											.toString()), true, 1));
				}
			}
			if (hit == 0) {
				table6.addCell(getTableCell(String.valueOf(j), true, 1));
				table6.addCell(getTableCell("", true, 1));
				table6.addCell(getTableCell("", true, 1));
				table6.addCell(getTableCell("", true, 1));
				table6.addCell(getTableCell("", true, 1));
				table6.addCell(getTableCell("", true, 1));
				table6.addCell(getTableCell("", true, 1));
				table6.addCell(getTableCell("", true, 1));
				table6.addCell(getTableCell("", true, 1));
				table6.addCell(getTableCell("", true, 1));
				table6.addCell(getTableCell("", true, 1));
			}
		}

		table6.addCell(getTableCell("", true, 2));
		if (pi.getBiMeasureSpecification().getRealPhoto1() != null) {
			StringUtils.writeFile(super.realFileDir
					+ pi.getBiMeasureSpecification().getRealPhoto1Name(), pi
					.getBiMeasureSpecification().getRealPhoto1());
			PdfPCell cell1 = new PdfPCell(Image.getInstance(super.realFileDir
					+ pi.getBiMeasureSpecification().getRealPhoto1Name()), true);
			cell1.setColspan(4);
			cell1.setPadding(15);
			table6.addCell(cell1);
		} else {
			table6.addCell(getTableCell("", true, 4));
		}

		table6.addCell(getTableCell("", true, 1));
		if (pi.getBiMeasureSpecification().getRealPhoto2() != null) {
			StringUtils.writeFile(super.realFileDir
					+ pi.getBiMeasureSpecification().getRealPhoto2Name(), pi
					.getBiMeasureSpecification().getRealPhoto2());
			PdfPCell cell2 = new PdfPCell(Image.getInstance(super.realFileDir
					+ pi.getBiMeasureSpecification().getRealPhoto2Name()), true);
			cell2.setColspan(4);
			cell2.setPadding(15);
			table6.addCell(cell2);
		} else {
			table6.addCell(getTableCell("", true, 4));
		}

		return table6;
	}

	private PdfPTable getTrimmingTable(ProductIdentification pi)
			throws Exception {
		float[] widthsTrimming = { 1f, 1f, 1.5f, 1f, 1f, 1f, 1f, 2.5f, 2f };
		PdfPTable table7 = new PdfPTable(widthsTrimming);
		table7.setWidthPercentage(100);

		PdfPCell cell10 = getTableCell("ui.productidentification.trimmings",
				true, 1);
		cell10.setRowspan(2);
		table7.addCell(cell10);

		PdfPCell cell11 = getTableCell("", true, 1);
		cell11.setRowspan(2);
		table7.addCell(cell11);

		PdfPCell cell12 = getTableCell("message.edit.reference", true, 1);
		cell12.setRowspan(2);
		table7.addCell(cell12);

		PdfPCell cell13 = getTableCell("ui.productidentification.color", true,
				1);
		cell13.setRowspan(2);
		table7.addCell(cell13);

		PdfPCell cell14 = getTableCell(
				"ui.productidentification.quantitysweater", true, 1);
		cell14.setRowspan(2);
		table7.addCell(cell14);

		table7.addCell(getTableCell("ui.accessory.supplier", true, 2));

		PdfPCell cell16 = getTableCell("ui.accessory.pose", true, 1);
		cell16.setRowspan(2);
		table7.addCell(cell16);

		PdfPCell cell17 = getTableCell("message.edit.photo", true, 1);
		cell17.setRowspan(2);
		table7.addCell(cell17);

		table7.addCell(getTableCell(new String[] {
				pi.getBiFranceCustomer().getName(),
				pi.getBiFranceCustomer().getDepartment() }, true, 1));
		table7.addCell(getTableCell("DZ", true, 1));

		Set<ProductAccessoryHang> phtSet = pi.getProductAccessoryHangs();
		Set<ProductAccessoryOthers> paoSet = pi.getProductAccessoryOtherses();
		for (ProductAccessoryHang pht : phtSet) {
			table7.addCell(getTableCell(this.ctService.getDescription(
					CtAccessoryType.class.getName(), pht
							.getBiAccessoryOthers1().getCtAccessoryType(),
					locale), true, 1));
			if (pht.getBiAccessoryOthers1().getPhoto1() != null) {
				StringUtils.writeFile(super.realFileDir
						+ pht.getBiAccessoryOthers1().getPhoto1Name(), pht
						.getBiAccessoryOthers1().getPhoto1());
				table7.addCell(Image.getInstance(super.realFileDir
						+ pht.getBiAccessoryOthers1().getPhoto1Name()));
			} else {
				table7.addCell(getTableCell("", true, 1));
			}
			table7.addCell(getTableCell(pht.getBiAccessoryOthers1()
					.getAccessoryReference(), true, 1));
			table7.addCell(getTableCell(pht.getBiAccessoryOthers1()
					.getCustomerColor(), true, 1));

			int hit = 0;
			for (ProductAccessoryOthers pao : paoSet) {
				if (pao.getBiAccessoryOthers().getId() == pht
						.getBiAccessoryOthers1().getId()) {
					hit++;
					table7.addCell(getTableCell(pao.getUnitNumber().toString(),
							true, 1));
					break;
				}
			}
			if (hit == 0) {
				table7.addCell(getTableCell("", true, 1));
			}

			if ("01".equals(pht.getBiAccessoryOthers1().getCtSupplyType())) {
				table7.addCell(getTableCell("X", true, 1));
				table7.addCell(getTableCell("", true, 1));
			} else {
				table7.addCell(getTableCell("", true, 1));
				table7.addCell(getTableCell("X", true, 1));
			}
			if (Constants.LOCALE_TYPE_EN.equals(locale)) {
				table7.addCell(getTableCell(pht.getDescriptionEN(), true, 1));
			} else if (Constants.LOCALE_TYPE_ZH.equals(locale)) {
				table7.addCell(getTableCell(pht.getDescriptionCN(), true, 1));
			} else {
				table7.addCell(getTableCell(pht.getDescriptionFR(), true, 1));
			}
			if (pht.getPhoto() != null) {
				StringUtils.writeFile(super.realFileDir + pht.getPhotoName(),
						pht.getPhoto());
				table7.addCell(Image.getInstance(super.realFileDir
						+ pht.getPhotoName()));
			} else {
				table7.addCell(getTableCell("", true, 1));
			}
		}
		return table7;
	}
	
	public static String pdfFileName(Integer id){
		ProductIdentificationDAO dao = new ProductIdentificationDAO();
		CtServiceImpl ctService = new CtServiceImpl();
		String pdfName = "";
		try {
			ProductIdentification pi = (ProductIdentification)dao.get(
					ProductIdentification.class.getName(), id);
			pdfName =  ctService.getDescription(CtSeasonNumber.class.getName(),
					pi.getCtSeasonNumber(), Constants.LOCALE_TYPE_FR) + "-" + pi.getReference() + "-" + pi.getBiFranceCustomer().getName();
			if(!StringUtils.isEmpty(pi.getBiFranceCustomer().getDepartment())){
				pdfName += "-" + pi.getBiFranceCustomer().getDepartment();
			}
			pdfName += "("+StringUtils.toStringLine(pi.getCreateDate()) + ")";
			return  pdfName;
		} catch (Exception e) {
			e.printStackTrace();
			pdfName = "TCoutput";
		}
		return pdfName;
	}

}
